﻿
using Bootstrap5.Abstractions;

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;

namespace Bootstrap5;

/// <summary>
/// Represents a dropdown item with HTML tag <c>&lt;li>...&lt;/li></c>. This is an abstract class.
/// </summary>
/// <typeparam name="TDropDownItem">The type of dropdown item component.</typeparam>
[HtmlTag("li")]
public abstract class DropDownItem<TDropDownItem> : BootstrapChildContentComponentBase
{
    [CascadingParameter] DropDownMenu CascadingDropDownMenu { get; set; }

    protected override void OnInitialized()
    {
        if (CascadingDropDownMenu is null || CascadingDropDownMenu.GetType() != typeof(DropDownMenu))
        {
            throw new CascadingValueNullException(typeof(TDropDownItem).Name, nameof(DropDownMenu));
        }
    }

    protected virtual string TagName { get; } = "div";

    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
        builder.OpenElement(0, GetHtmlTagName());
        builder.AddContent(1, BuildChildRenderTree);
        builder.CloseElement();
    }

    protected virtual void BuildChildRenderTree(RenderTreeBuilder builder)
    {
        builder.OpenElement(0, TagName);
        BuildCommonAttributes(builder);
        builder.AddContent(1,ChildContent);
        builder.CloseElement();
    }
}
